﻿Public Class CustomCommands
    Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
        Dim openDialog As New OpenFileDialog
        txtFileName.Text = ""
        openDialog.Filter = "Executables or Music|*.COM;*.EXE;*.BAT;*.CMD;*.VBS;*.BAT;*.AHK;*.PY;*.wpl;*.m3u;*.mp3;*.wma;*.wav;*.xspf;*.html;*.txt|All Files you want|*.*"
        openDialog.Title = "Select the file you want to execute:"
        openDialog.ShowDialog()
        If openDialog.FileName <> "" Then
            txtFileName.Text = openDialog.FileName
        Else
            txtFileName.Text = "local file path..."
        End If
        openDialog.Dispose()
    End Sub

    Private Sub btnAddCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCommand.Click


        If txtCommand.Text.Trim = "" Or txtFileName.Text.Trim = "" Then
            MsgBox("Please enter Command and File.")
            txtCommand.Focus()
        Else
            Dim preDefinedCommand() As String = {"shutdown", "logoff", "restart", "physical memory", "virtual memory", "os", "ip", "lock", "standby", "hibernate", "screenshot", "getprocesslist", "getfile", "message", "help"}
            Dim i As Integer
            For i = 0 To preDefinedCommand.Length - 1
                If LCase(txtCommand.Text.Trim) = preDefinedCommand(i) Then
                    MsgBox("Hey! that command is reserved.")
                    txtCommand.Focus()
                    Exit Sub
                End If
            Next

            Dim splitCommand() As String
            splitCommand = txtCommand.Text.Trim.Split(" ")

            If splitCommand.Length > 1 Then
                MsgBox("Sorry, but the Command can not be more than one word.")
                txtCommand.Focus()
                Exit Sub
            End If

            Call insertVaue(LCase(txtCommand.Text.Trim), txtFileName.Text.Trim)
            Call displayCustomCommand()
            txtCommand.Text = ""
            txtFileName.Text = ""
        End If
    End Sub

    Private Sub CustomCommands_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call displayCustomCommand()
    End Sub

    Sub displayCustomCommand()
        frmTweetMyPc.cmd.Connection = frmTweetMyPc.con
        Call ConnectDB()
        Dim SqlDs As New DataSet
        Dim SqlAdapter As New OleDb.OleDbDataAdapter(frmTweetMyPc.cmd)
        Try
            frmTweetMyPc.cmd.CommandText = "SELECT * FROM CustomCommand"
            SqlAdapter.Fill(SqlDs)
            dtCustomCommads.DataSource = SqlDs.Tables(0).DefaultView
            dtCustomCommads.Refresh()
            dtCustomCommads.RowHeadersVisible = False
            dtCustomCommads.Columns(0).Width = 185
            dtCustomCommads.Columns(1).Width = 250
            Call DisconnectDB()
        Catch ex As Exception
            Call DisconnectDB()
        End Try
        'SqlDs.Dispose()
        SqlAdapter.Dispose()
    End Sub

    Sub ConnectDB()
        Try
            frmTweetMyPc.con.Open()
            frmTweetMyPc.cmd = frmTweetMyPc.con.CreateCommand
        Catch ex As Exception
            MsgBox(ex.Message + "Please try again later.")
        End Try
    End Sub

    Sub DisconnectDB()
        frmTweetMyPc.con.Close()
        frmTweetMyPc.cmd.Dispose()
    End Sub

    Sub insertVaue(ByVal command As String, ByVal file As String)
        Call ConnectDB()
        Dim SqlAdapter As New OleDb.OleDbDataAdapter(frmTweetMyPc.cmd)
        Dim sqlDs As New DataSet
        frmTweetMyPc.cmd.CommandText = "SELECT * FROM CustomCommand WHERE Command='" + LCase(command) + "' "
        Try
            frmTweetMyPc.cmd.ExecuteNonQuery()
            SqlAdapter.Fill(sqlDs)
            If sqlDs.Tables(0).Rows.Count = 0 Then
                frmTweetMyPc.cmd.CommandText = ""
                frmTweetMyPc.cmd.CommandText = "INSERT INTO CustomCommand (Command, File) VALUES ('" + LCase(command) + "','" + file + "')"
                frmTweetMyPc.cmd.ExecuteNonQuery()
            Else
                frmTweetMyPc.cmd.CommandText = "UPDATE CustomCommand SET File='" + file + "' WHERE Command='" + LCase(command) + "'"
                frmTweetMyPc.cmd.ExecuteNonQuery()
            End If
        Catch ex As Exception
            MsgBox(ex.Message + ".Please try again.")
        End Try
        SqlAdapter.Dispose()
        sqlDs.Dispose()
        Call DisconnectDB()
    End Sub

    Private Sub btnRemoveCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveCommand.Click
        If (dtCustomCommads.RowCount.ToString() <> 0) Then
            If dtCustomCommads.CurrentCell.Value <> "" Then
                Call deleteValues(dtCustomCommads.CurrentRow.Cells(0).Value.ToString)
            End If
            Call displayCustomCommand()
        End If
    End Sub

    Sub deleteValues(ByVal command As String)
        Call ConnectDB()
        frmTweetMyPc.cmd.CommandText = "DELETE FROM CustomCommand WHERE Command='" + LCase(command) + "' "
        Try
            frmTweetMyPc.cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Call DisconnectDB()
    End Sub
End Class